package com.bitcat.servicedemo;

import android.animation.ObjectAnimator;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.imageview.ShapeableImageView;

public class MusicActivity extends AppCompatActivity {

    private ServiceConnection conn;
    private MusicService.MyBinder musicControl;
    private ShapeableImageView musicIv;
    private ObjectAnimator animator;
    private SeekBar seekBar;
    private TextView currentTv,totalTv;
    private MyReceiver receiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_music);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        seekBar = findViewById(R.id.seekbar);
        currentTv = findViewById(R.id.currentTv);
        totalTv = findViewById(R.id.totalTv);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                musicControl = (MusicService.MyBinder) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };


        bindService(new Intent(this, MusicService.class), conn, BIND_AUTO_CREATE);


        musicIv = findViewById(R.id.musicIv);
        animator = ObjectAnimator.ofFloat(musicIv,"rotation",0F,360F);
        animator.setDuration(10000);//时长
        animator.setInterpolator(new LinearInterpolator());  //线性
        animator.setRepeatCount(-1);//无限循环


        receiver = new MyReceiver();
        registerReceiver(receiver,new IntentFilter("update"));


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress == seekBar.getMax()){
                    animator.pause();
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                musicControl.seekTo(seekBar.getProgress());
            }
        });



    }

    //开始播放
    public void start(View view) {
        musicControl.start();
        animator.start();
    }

    //暂停播放
    public void pause(View view) {
        musicControl.pause();
        animator.pause();

    }

    //继续播放
    public void continuePlay(View view) {
        musicControl.continuePlay();
        animator.start();
    }

    //退出
    public void exit(View view) {
        musicControl.exit();
        animator.pause();
        unbindService(conn);
        finish();
    }








    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if ("update".equals(intent.getAction())){
                int duration = intent.getIntExtra("duration",0);
                int currentPosition = intent.getIntExtra("currentPosition",0);
                seekBar.setMax(duration);
                seekBar.setProgress(currentPosition);

                currentTv.setText(TimeUtils.getTime(currentPosition));
                totalTv.setText(TimeUtils.getTime(duration));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}